home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / crefc.zip / CREFC.C < prev    next >
Text File  |  1993-01-04  |  5KB  |  177 lines

  1. #define YES 1
  2. #define NO 0
  3. #include "stdio.h"
  4. #include "ctype.h"
  5. struct words {
  6.     char symbol[15];
  7.     struct words *left;    /*left daughter*/
  8.     struct words *right;    /*right daughter*/
  9.     struct lines *lineptr;    /*pointer to linked list of line numbers*/
  10. };
  11. struct lines {
  12.     int lineno;    /*line numbers*/
  13.     struct lines *nxtline;    /*pointer to next structure*/
  14. };
  15. main(number,name)
  16. int number;
  17. char *name[];
  18. {
  19.     FILE *infile, *outfile;
  20.     char word[20],*ptr;
  21.     int lineno,incomm,inword,instr,c,pchar,mon,year,day;        
  22.     struct words *rootword,*saveword();
  23.     rootword=NULL;    /*first pointer to the root of the tree*/
  24.     
  25.     if(number<2)
  26.     {
  27.         printf("No file specified");
  28.         exit();
  29.     }
  30.     if((infile=fopen(name[1],"r"))==NULL)
  31.     {
  32.         printf("Can't open %s",name[1]);
  33.         exit();
  34.     }
  35.     if((outfile=fopen(name[2],"w"))==NULL)
  36.     {
  37.         printf("Can't open %s",name[2]);
  38.         exit();
  39.     }
  40.     
  41.     ptr=word;
  42.     incomm=NO;
  43.     instr=NO;
  44.     lineno=1;
  45.     inword=NO;
  46.     while((c=fgetc(infile))!=EOF)
  47.     {
  48.         if(c=='\042'&&instr)    /*is it a quote*/
  49.             instr=NO;
  50.         else if(c=='\042'&&!instr)
  51.             instr=YES;
  52.         if(c=='*' && pchar=='/')    /*is it a comment*/
  53.         {
  54.             incomm=YES;
  55.             pchar=c;
  56.             continue;
  57.         }
  58.         else if(c=='/' && pchar=='*')
  59.         {
  60.             incomm=NO;
  61.             pchar=c;
  62.             continue;
  63.         }
  64. /*If this is true it is the end of a word*/
  65.         if(!instr&&!incomm&&(ispunct(c)||isspace(c)||c=='\n'))
  66.         {
  67.             *ptr='\0';    /*zap null to end of string*/
  68.             if(strcmp(word,"")!=0 && isalnum(pchar))
  69.             {
  70.                 if(!iskey(word))    /*is is a keyword*/
  71.             /*put the word in the tree*/
  72.                     rootword=saveword(word,lineno,rootword);
  73.                 ptr=word;    /*reset pointer*/
  74.             }
  75.         }
  76. /*if this is true add the character to the string*/
  77.         else if(!instr&&!incomm&&isalnum(c))
  78.             *ptr++=c;
  79.         if(c=='\n')    /*newline, so increment line counter*/
  80.             lineno++;
  81.         pchar=c;
  82.     }
  83.     sysdate(&mon,&day,&year);    /*get sysdate for printer*/
  84.     fprintf(outfile,"SYMBOL CROSS REFERENCE\n");
  85.     fprintf(outfile,"File Name: %s\n",name[1]);
  86.     fprintf(outfile,"Date printed: %d/%d/%d\n",mon,day,year);
  87.     fprintf(outfile,"\n\nSYMBOL   LINES WHERE SYMBOL IS FOUND\n");
  88.     display(rootword,outfile);    /*display the tree*/
  89. }
  90. struct words *saveword(w,line,p)/*put the words in a tree*/
  91. struct words *p;
  92. char *w;
  93. int line;
  94. {
  95.     struct words *wrdalloc();
  96.     struct lines *linealloc(),*addline();
  97.     int cond;
  98.  
  99.     if(p==NULL)    /*end of the branch*/
  100.     {
  101.         p=wrdalloc();    /*allocate some memory for the structure*/
  102.         strcpy(p->symbol,w);
  103.         p->left=p->right=NULL;    /*set new daughters*/
  104.         p->lineptr=linealloc();    /*get structure pointer for lineno*/
  105.         p->lineptr->lineno=line;/*set lineno*/
  106.         p->lineptr->nxtline=NULL;/*set nextline number pointer*/
  107.     }
  108.     else if((cond=strcmp(w,p->symbol))==0)/*does the word already exist*/
  109.         p->lineptr=addline(p->lineptr,line);/*add the line number*/
  110.     else if(cond<0)    /*look in the left branch*/
  111.         p->left=saveword(w,line,p->left);
  112.     else 
  113.         p->right=saveword(w,line,p->right);/*look in the right branch*/
  114.     return(p);
  115. }
  116. struct words *wrdalloc()    /*memory allocation for the words structure*/
  117. {
  118.     char *getmem();
  119.     return((struct words *)getmem(sizeof(struct words)));
  120. }
  121. struct lines *addline(ptr,line)    /*add a new line to the list of lines*/
  122. struct lines *ptr;
  123. int line;
  124. {
  125.     struct lines *linealloc();
  126.     if(ptr==NULL)    /*is it the end of the list*/
  127.     {
  128.         ptr=linealloc();    /*allocate some memory*/
  129.         ptr->nxtline=NULL;    /*set the pointers and line number*/
  130.         ptr->lineno=line;
  131.     }
  132.     else    /*look for the end of the list*/
  133.         ptr->nxtline=addline(ptr->nxtline,line);
  134.     return(ptr);
  135. }
  136. struct lines *linealloc()    /*memory allocator for the lines structure*/
  137. {
  138.     char *getmem();
  139.     return((struct lines *)getmem(sizeof(struct lines)));
  140. }
  141. display(p,outf)    /*displays the tree*/
  142. FILE *outf;
  143. struct words *p;
  144. {
  145.     struct lines *lptr;
  146.     int i;
  147.     if(p!=NULL)
  148.     {
  149.         display(p->left,outf);    /*display the left tree*/
  150.         fprintf(outf,"%-8.8s ",p->symbol);
  151.             for(lptr=p->lineptr,i=1;lptr!=NULL;lptr=lptr->nxtline,i++)
  152.         {
  153.         fprintf(outf,"%4d ",lptr->lineno);    /*display the line numbers*/
  154.            if(i%14==0&&lptr->nxtline!=NULL)
  155.             fprintf(outf,"%-8s  "," ");
  156.         }           
  157.         fputc('\n',outf);
  158.         display(p->right,outf);    /*display the right tree*/
  159.     }
  160. }
  161. /*Check if the word is a C keyword*/
  162. iskey(word)
  163. char *word;
  164. {
  165.     static char *keyword[]={
  166. "int","extern","else","char","register","for","float","typedef","do","double",
  167. "static","while","struct","goto","switch","union","return","case","long",
  168. "sizeof","default","short","break","entry","unsigned","continue","auto",
  169. "if","include","define"};
  170.  
  171.     int i;
  172.     for(i=0;i<sizeof keyword/sizeof(char *);i++)
  173.         if(strcmp(keyword[i],word)==0)
  174.             return(1);
  175.     return(0);
  176. }
  177.